using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Document;

namespace DukeyCssFetch
{
	// Token: 0x0200000B RID: 11
	public class FindAndReplaceForm : Form
	{
		// Token: 0x0600006E RID: 110 RVA: 0x000096C0 File Offset: 0x000078C0
		public FindAndReplaceForm()
		{
			this.dictionary_0 = new Dictionary<TextEditorControl, HighlightGroup>();
			
			this.InitializeComponent();
			this.textEditorSearcher_0 = new TextEditorSearcher();
		}

		// Token: 0x0600007B RID: 123 RVA: 0x00009C44 File Offset: 0x00007E44
		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		// Token: 0x06000079 RID: 121 RVA: 0x00009ADC File Offset: 0x00007CDC
		private void btnFindAllHighlightAll_Click(object sender, EventArgs e)
		{
			if (!this.dictionary_0.ContainsKey(this.textEditorControl_0))
			{
				this.dictionary_0[this.textEditorControl_0] = new HighlightGroup(this.textEditorControl_0);
			}
			HighlightGroup highlightGroup = this.dictionary_0[this.textEditorControl_0];
			if (string.IsNullOrEmpty(this.LookFor))
			{
				highlightGroup.ClearMarkers();
				return;
			}
			this.textEditorSearcher_0.LookFor = this.txtLookFor.Text;
			this.textEditorSearcher_0.MatchCase = this.chkMatchCase.Checked;
			this.textEditorSearcher_0.MatchWholeWordOnly = this.chkMatchWholeWord.Checked;
			bool flag = false;
			int beginAtOffset = 0;
			int num = 0;
			while (true)
			{
				TextRange textRange = this.textEditorSearcher_0.FindNext(beginAtOffset, false, out flag);
				if (textRange == null || flag)
				{
					break;
				}
				beginAtOffset = textRange.Offset + textRange.Length;
				num++;
				TextMarker marker = new TextMarker(textRange.Offset, textRange.Length, TextMarkerType.SolidBlock, Color.Yellow, Color.Black);
				highlightGroup.AddMarker(marker);
			}
			if (num == 0)
			{
				MessageBox.Show("没有找到你要查找的内容", "提示");
				return;
			}
			base.Close();
		}

		// Token: 0x06000076 RID: 118 RVA: 0x00009930 File Offset: 0x00007B30
		private void btnFindNext_Click(object sender, EventArgs e)
		{
			this.FindNext(false, false, "内容没有找到");
		}

		// Token: 0x06000075 RID: 117 RVA: 0x00009920 File Offset: 0x00007B20
		private void btnFindPrevious_Click(object sender, EventArgs e)
		{
			this.FindNext(false, true, "内容没有找到！");
		}

		// Token: 0x0600007D RID: 125 RVA: 0x00009CA8 File Offset: 0x00007EA8
		private void btnReplaceAll_Click(object sender, EventArgs e)
		{
			int num = 0;
			this.textEditorControl_0.ActiveTextAreaControl.Caret.Position = this.textEditorControl_0.Document.OffsetToPosition(this.textEditorSearcher_0.BeginOffset);
			lock (this.textEditorControl_0.Document.UndoStack)
			{
				while (this.FindNext(false, false, null) != null && !this._lastSearchLoopedAround)
				{
					num++;
					this.method_2(this.txtReplaceWith.Text);
				}
			}
			if (num == 0)
			{
				MessageBox.Show(string.Format("没有找到目标内容: {0}！", this.txtLookFor.Text), "提示");
				return;
			}
			MessageBox.Show(string.Format("替换了 {0} 处内容！", num), "提示");
			base.Close();
		}

		// Token: 0x0600007C RID: 124 RVA: 0x00009C4C File Offset: 0x00007E4C
		private void btnReplace_Click(object sender, EventArgs e)
		{
			SelectionManager selectionManager = this.textEditorControl_0.ActiveTextAreaControl.SelectionManager;
			if (string.Equals(selectionManager.SelectedText, this.txtLookFor.Text, StringComparison.OrdinalIgnoreCase))
			{
				this.method_2(this.txtReplaceWith.Text);
			}
			this.FindNext(false, this._lastSearchWasBackward, "内容没有找到！");
		}

		// Token: 0x06000080 RID: 128 RVA: 0x00009E24 File Offset: 0x00008024
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.icontainer_0 != null)
			{
				this.icontainer_0.Dispose();
			}
			base.Dispose(disposing);
		}

		// Token: 0x0600007A RID: 122 RVA: 0x00009BF8 File Offset: 0x00007DF8
		private void FindAndReplaceForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (e.CloseReason != CloseReason.FormOwnerClosing)
			{
				if (base.Owner != null)
				{
					base.Owner.Select();
				}
				e.Cancel = true;
				base.Hide();
				this.textEditorSearcher_0.ClearScanRegion();
				this.textEditorControl_0.Refresh();
			}
		}

		// Token: 0x06000077 RID: 119 RVA: 0x00009940 File Offset: 0x00007B40
		public TextRange FindNext(bool viaF3, bool searchBackward, string messageIfNotFound)
		{
			if (string.IsNullOrEmpty(this.txtLookFor.Text))
			{
				MessageBox.Show("没有指定要查找的内容！", "提示");
				return null;
			}
			this._lastSearchWasBackward = searchBackward;
			this.textEditorSearcher_0.LookFor = this.txtLookFor.Text;
			this.textEditorSearcher_0.MatchCase = this.chkMatchCase.Checked;
			this.textEditorSearcher_0.MatchWholeWordOnly = this.chkMatchWholeWord.Checked;
			Caret caret = this.textEditorControl_0.ActiveTextAreaControl.Caret;
			if (viaF3 && this.textEditorSearcher_0.HasScanRegion && !Globals.IsInRange(caret.Offset, this.textEditorSearcher_0.BeginOffset, this.textEditorSearcher_0.EndOffset))
			{
				this.textEditorSearcher_0.ClearScanRegion();
				this.method_0();
			}
			int beginAtOffset = caret.Offset - (searchBackward ? 1 : 0);
			TextRange textRange = this.textEditorSearcher_0.FindNext(beginAtOffset, searchBackward, out this._lastSearchLoopedAround);
			if (textRange != null)
			{
				this.method_1(textRange);
			}
			else if (messageIfNotFound != null)
			{
				MessageBox.Show(messageIfNotFound, "提示");
			}
			return textRange;
		}

		// Token: 0x06000081 RID: 129 RVA: 0x00009E44 File Offset: 0x00008044
		private void InitializeComponent()
		{
			this.label1 = new Label();
			this.lblReplaceWith = new Label();
			this.txtLookFor = new TextBox();
			this.txtReplaceWith = new TextBox();
			this.btnFindNext = new Button();
			this.btnReplace = new Button();
			this.btnReplaceAll = new Button();
			this.chkMatchWholeWord = new CheckBox();
			this.chkMatchCase = new CheckBox();
			this.btnFindAllHighlightAll = new Button();
			this.btnCancel = new Button();
			this.btnFindPrevious = new Button();
			this.groupBox1 = new GroupBox();
			this.groupBox1.SuspendLayout();
			base.SuspendLayout();
			this.label1.AutoSize = true;
			this.label1.Location = new Point(12, 9);
			this.label1.Name = "label1";
			this.label1.Size = new Size(77, 12);
			this.label1.TabIndex = 0;
			this.label1.Text = "查找内容(&N):";
			this.lblReplaceWith.AutoSize = true;
			this.lblReplaceWith.Location = new Point(12, 36);
			this.lblReplaceWith.Name = "lblReplaceWith";
			this.lblReplaceWith.Size = new Size(65, 12);
			this.lblReplaceWith.TabIndex = 2;
			this.lblReplaceWith.Text = "替换为(&P):";
			this.txtLookFor.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.txtLookFor.Location = new Point(95, 6);
			this.txtLookFor.Name = "txtLookFor";
			this.txtLookFor.Size = new Size(232, 21);
			this.txtLookFor.TabIndex = 1;
			this.txtReplaceWith.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.txtReplaceWith.Location = new Point(95, 33);
			this.txtReplaceWith.Name = "txtReplaceWith";
			this.txtReplaceWith.Size = new Size(232, 21);
			this.txtReplaceWith.TabIndex = 3;
			this.btnFindNext.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
			this.btnFindNext.Location = new Point(228, 137);
			this.btnFindNext.Name = "btnFindNext";
			this.btnFindNext.Size = new Size(100, 30);
			this.btnFindNext.TabIndex = 6;
			this.btnFindNext.Text = "查找下一个(&F)";
			this.btnFindNext.UseVisualStyleBackColor = true;
			this.btnFindNext.Click += new EventHandler(this.btnFindNext_Click);
			this.btnReplace.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
			this.btnReplace.Location = new Point(16, 173);
			this.btnReplace.Name = "btnReplace";
			this.btnReplace.Size = new Size(100, 30);
			this.btnReplace.TabIndex = 7;
			this.btnReplace.Text = "替换(&R)";
			this.btnReplace.UseVisualStyleBackColor = true;
			this.btnReplace.Click += new EventHandler(this.btnReplace_Click);
			this.btnReplaceAll.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
			this.btnReplaceAll.Location = new Point(122, 173);
			this.btnReplaceAll.Name = "btnReplaceAll";
			this.btnReplaceAll.Size = new Size(100, 30);
			this.btnReplaceAll.TabIndex = 9;
			this.btnReplaceAll.Text = "全部替换(&A)";
			this.btnReplaceAll.UseVisualStyleBackColor = true;
			this.btnReplaceAll.Click += new EventHandler(this.btnReplaceAll_Click);
			this.chkMatchWholeWord.AutoSize = true;
			this.chkMatchWholeWord.Location = new Point(145, 27);
			this.chkMatchWholeWord.Name = "chkMatchWholeWord";
			this.chkMatchWholeWord.Size = new Size(90, 16);
			this.chkMatchWholeWord.TabIndex = 5;
			this.chkMatchWholeWord.Text = "全字匹配(&W)";
			this.chkMatchWholeWord.UseVisualStyleBackColor = true;
			this.chkMatchCase.AutoSize = true;
			this.chkMatchCase.Location = new Point(22, 27);
			this.chkMatchCase.Name = "chkMatchCase";
			this.chkMatchCase.Size = new Size(102, 16);
			this.chkMatchCase.TabIndex = 4;
			this.chkMatchCase.Text = "大小写匹配(&C)";
			this.chkMatchCase.UseVisualStyleBackColor = true;
			this.btnFindAllHighlightAll.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
			this.btnFindAllHighlightAll.Location = new Point(72, 173);
			this.btnFindAllHighlightAll.Name = "btnFindAllHighlightAll";
			this.btnFindAllHighlightAll.Size = new Size(150, 30);
			this.btnFindAllHighlightAll.TabIndex = 8;
			this.btnFindAllHighlightAll.Text = "查找全部并高亮显示(&H)";
			this.btnFindAllHighlightAll.UseVisualStyleBackColor = true;
			this.btnFindAllHighlightAll.Visible = false;
			this.btnFindAllHighlightAll.Click += new EventHandler(this.btnFindAllHighlightAll_Click);
			this.btnCancel.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
			this.btnCancel.DialogResult = DialogResult.Cancel;
			this.btnCancel.Location = new Point(228, 173);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new Size(100, 30);
			this.btnCancel.TabIndex = 6;
			this.btnCancel.Text = "取消(&C)";
			this.btnCancel.UseVisualStyleBackColor = true;
			this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
			this.btnFindPrevious.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
			this.btnFindPrevious.Location = new Point(122, 137);
			this.btnFindPrevious.Name = "btnFindPrevious";
			this.btnFindPrevious.Size = new Size(100, 30);
			this.btnFindPrevious.TabIndex = 6;
			this.btnFindPrevious.Text = "查找上一个(&P)";
			this.btnFindPrevious.UseVisualStyleBackColor = true;
			this.btnFindPrevious.Click += new EventHandler(this.btnFindPrevious_Click);
			this.groupBox1.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.groupBox1.Controls.Add(this.chkMatchCase);
			this.groupBox1.Controls.Add(this.chkMatchWholeWord);
			this.groupBox1.Location = new Point(14, 64);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new Size(313, 63);
			this.groupBox1.TabIndex = 10;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "查找选项";
			base.AcceptButton = this.btnReplace;
			base.AutoScaleDimensions = new SizeF(6f, 12f);
			base.AutoScaleMode = AutoScaleMode.Font;
			base.CancelButton = this.btnCancel;
			base.ClientSize = new Size(339, 215);
			base.Controls.Add(this.groupBox1);
			base.Controls.Add(this.btnReplaceAll);
			base.Controls.Add(this.btnReplace);
			base.Controls.Add(this.btnFindAllHighlightAll);
			base.Controls.Add(this.btnCancel);
			base.Controls.Add(this.btnFindPrevious);
			base.Controls.Add(this.btnFindNext);
			base.Controls.Add(this.txtReplaceWith);
			base.Controls.Add(this.txtLookFor);
			base.Controls.Add(this.lblReplaceWith);
			base.Controls.Add(this.label1);
			base.FormBorderStyle = FormBorderStyle.FixedDialog;
			base.MaximizeBox = false;
			base.MinimizeBox = false;
			base.Name = "FindAndReplaceForm";
			base.ShowIcon = false;
			base.ShowInTaskbar = false;
			base.StartPosition = FormStartPosition.CenterScreen;
			this.Text = "查找和替换";
			base.FormClosing += new FormClosingEventHandler(this.FindAndReplaceForm_FormClosing);
			this.groupBox1.ResumeLayout(false);
			this.groupBox1.PerformLayout();
			base.ResumeLayout(false);
			base.PerformLayout();
		}

		// Token: 0x06000071 RID: 113 RVA: 0x0000971C File Offset: 0x0000791C
		private void method_0()
		{
			string text = this.ReplaceMode ? "查找和替换" : "查找";
			if (this.textEditorControl_0 != null && this.textEditorControl_0.FileName != null)
			{
				text = text + " - " + Path.GetFileName(this.textEditorControl_0.FileName);
			}
			if (this.textEditorSearcher_0.HasScanRegion)
			{
				text += " (只可选)";
			}
			this.Text = text;
		}

		// Token: 0x06000078 RID: 120 RVA: 0x00009A50 File Offset: 0x00007C50
		private void method_1(TextRange textRange_0)
		{
			TextLocation startPosition = this.textEditorControl_0.Document.OffsetToPosition(textRange_0.Offset);
			TextLocation endPosition = this.textEditorControl_0.Document.OffsetToPosition(textRange_0.Offset + textRange_0.Length);
			this.textEditorControl_0.ActiveTextAreaControl.SelectionManager.SetSelection(startPosition, endPosition);
			this.textEditorControl_0.ActiveTextAreaControl.Caret.Position = this.textEditorControl_0.Document.OffsetToPosition(textRange_0.Offset + textRange_0.Length);
		}

		// Token: 0x0600007E RID: 126 RVA: 0x00009D88 File Offset: 0x00007F88
		private void method_2(string string_0)
		{
			TextArea textArea = this.textEditorControl_0.ActiveTextAreaControl.TextArea;
			lock (textArea.Document.UndoStack)
			{
				if (textArea.SelectionManager.HasSomethingSelected)
				{
					textArea.Caret.Position = textArea.SelectionManager.SelectionCollection[0].StartPosition;
					textArea.SelectionManager.RemoveSelectedText();
				}
				textArea.InsertString(string_0);
			}
		}

		// Token: 0x06000072 RID: 114 RVA: 0x00009790 File Offset: 0x00007990
		public void ShowFor(TextEditorControl editor, bool replaceMode)
		{
			this.Editor = editor;
			this.textEditorSearcher_0.ClearScanRegion();
			SelectionManager selectionManager = editor.ActiveTextAreaControl.SelectionManager;
			if (selectionManager.HasSomethingSelected && selectionManager.SelectionCollection.Count == 1)
			{
				ISelection selection = selectionManager.SelectionCollection[0];
				if (selection.StartPosition.Y == selection.EndPosition.Y)
				{
					this.txtLookFor.Text = selectionManager.SelectedText;
				}
				else
				{
					this.textEditorSearcher_0.SetScanRegion(selection);
				}
			}
			else
			{
				Caret caret = editor.ActiveTextAreaControl.Caret;
				int num = TextUtilities.FindWordStart(editor.Document, caret.Offset);
				int num2 = TextUtilities.FindWordEnd(editor.Document, caret.Offset);
				this.txtLookFor.Text = editor.Document.GetText(num, num2 - num);
			}
			this.ReplaceMode = replaceMode;
			base.Owner = (Form)editor.TopLevelControl;
			base.Show();
			this.txtLookFor.SelectAll();
			this.txtLookFor.Focus();
		}

		// Token: 0x17000001 RID: 1
		private TextEditorControl Editor
		{
			// Token: 0x0600006F RID: 111 RVA: 0x000096EC File Offset: 0x000078EC
			get
			{
				return this.textEditorControl_0;
			}
			// Token: 0x06000070 RID: 112 RVA: 0x000096F4 File Offset: 0x000078F4
			set
			{
				this.textEditorControl_0 = value;
				this.textEditorSearcher_0.Document = this.textEditorControl_0.Document;
				this.method_0();
			}
		}

		// Token: 0x17000003 RID: 3
		public string LookFor
		{
			// Token: 0x0600007F RID: 127 RVA: 0x00009E14 File Offset: 0x00008014
			get
			{
				return this.txtLookFor.Text;
			}
		}

		// Token: 0x17000002 RID: 2
		public bool ReplaceMode
		{
			// Token: 0x06000073 RID: 115 RVA: 0x000098A0 File Offset: 0x00007AA0
			get
			{
				return this.txtReplaceWith.Visible;
			}
			// Token: 0x06000074 RID: 116 RVA: 0x000098B0 File Offset: 0x00007AB0
			set
			{
				Control arg_15_0 = this.btnReplace;
				this.btnReplaceAll.Visible = value;
				arg_15_0.Visible = value;
				Control arg_2F_0 = this.lblReplaceWith;
				this.txtReplaceWith.Visible = value;
				arg_2F_0.Visible = value;
				this.btnFindAllHighlightAll.Visible = !value;
				base.AcceptButton = (value ? this.btnReplace : this.btnFindNext);
				this.method_0();
			}
		}

		// Token: 0x040000CB RID: 203
		private Button btnCancel;

		// Token: 0x040000CA RID: 202
		private Button btnFindAllHighlightAll;

		// Token: 0x040000C5 RID: 197
		private Button btnFindNext;

		// Token: 0x040000CC RID: 204
		private Button btnFindPrevious;

		// Token: 0x040000C6 RID: 198
		private Button btnReplace;

		// Token: 0x040000C7 RID: 199
		private Button btnReplaceAll;

		// Token: 0x040000C9 RID: 201
		private CheckBox chkMatchCase;

		// Token: 0x040000C8 RID: 200
		private CheckBox chkMatchWholeWord;

		// Token: 0x040000BF RID: 191
		private Dictionary<TextEditorControl, HighlightGroup> dictionary_0;

		// Token: 0x040000CD RID: 205
		private GroupBox groupBox1;

		// Token: 0x040000C0 RID: 192
		private IContainer icontainer_0;

		// Token: 0x040000C1 RID: 193
		private Label label1;

		// Token: 0x040000C2 RID: 194
		private Label lblReplaceWith;

		// Token: 0x040000BC RID: 188
		private TextEditorControl textEditorControl_0;

		// Token: 0x040000BB RID: 187
		private TextEditorSearcher textEditorSearcher_0;

		// Token: 0x040000C3 RID: 195
		private TextBox txtLookFor;

		// Token: 0x040000C4 RID: 196
		private TextBox txtReplaceWith;

		// Token: 0x040000BE RID: 190
		public bool _lastSearchLoopedAround;

		// Token: 0x040000BD RID: 189
		public bool _lastSearchWasBackward;
	}
}
